﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace Manager
{
    public partial class frmPB : Form
    {
        SQL sql = new SQL();
        string MPB = null;
        string NPB = null;
        string TP = null;
        string nameTP = null;
        string MCT = null;
        string PT = null;
        string strQuery;
        int index;
        public frmPB()
        {
            InitializeComponent();
        }

        private void frmPB_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            btnAddPB.Enabled = false;
            btnDeletePB.Enabled = false;
            btnEditPB.Enabled = false;
            btnUpdatePB.Enabled = false;
            getMPB();
            getMTP();
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Search();              
        }
        private void View()
        {  
            string strQuery = "sql_select_PhongBan";
            dataGridView1.DataSource = sql.getDataTable(strQuery);
        }
        private void updateNhanVien_TP(string preMTP,string MPB,string MNV)
        {
            try
            {
                string MTP = "TP" + MPB.Substring(2);
                string query = "sql_update_NVtoTP";
                string tmp = "preMTP";

                SqlParameter[] newPr = new SqlParameter[2];
                newPr[0] = new SqlParameter("@MNV", preMTP);
                newPr[1] = new SqlParameter("@MTP", tmp);
                sql.execute_Query(query, newPr);


                SqlParameter[] pr = new SqlParameter[2];
                pr[0] = new SqlParameter("@MNV", MNV);
                pr[1] = new SqlParameter("@MTP", MTP);
                sql.execute_Query(query, pr);

                SqlParameter[] pr1 = new SqlParameter[2];
                pr1[0] = new SqlParameter("@MNV", tmp);
                pr1[1] = new SqlParameter("@MTP", MNV);
                sql.execute_Query(query, pr1);
                MessageBox.Show("Execute successful..", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch 
            {
                MessageBox.Show("Please choose Phòng ban !", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void Search()
        {
            MPB = cbSearchMPB.Text;
            NPB = tbSearchNPB.Text;
            TP = cbSearchMTP.Text; 
            nameTP = txtSearchNAMETP.Text;
            if ((tbSearchNPB.Text == "") && (cbSearchMPB.Text == "") && (cbSearchMTP.Text == "")&& (txtSearchNAMETP.Text==""))
            {
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                View();
            }
            else
            {
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                string query = "sql_search_PhongBan";
                SqlParameter[] sqlPara = new SqlParameter[4];
                sqlPara[0] = new SqlParameter("@MPB", cbSearchMPB.Text.Trim());
                sqlPara[1] = new SqlParameter("@MTP", cbSearchMTP.Text.Trim());
                sqlPara[2] = new SqlParameter("@NAMEPB", tbSearchNPB.Text);
                sqlPara[3] = new SqlParameter("@NAMETP", txtSearchNAMETP.Text);
                DataTable dt = sql.getDataTable(query, sqlPara);
                dt.Columns[0].ColumnName = "Mã phòng ban";
                dt.Columns[1].ColumnName = "Tên phòng ban";
                dt.Columns[2].ColumnName = "Công việc";
                dt.Columns[3].ColumnName = "Mã nhân viên";
                dt.Columns[4].ColumnName = "Tên nhân viên";
                dt.Columns[5].ColumnName = "Quê quán";
                dt.Columns[6].ColumnName = "Ngày vào công ty";
                dt.Columns[7].ColumnName = "Ngày sinh";
                dt.Columns[8].ColumnName = "Mã công trình";
                dt.Columns[9].ColumnName = "Tên công trình";
                dt.Columns[10].ColumnName = "Ngày cấp phép";
                dt.Columns[11].ColumnName = "Ngày khởi công";
                dt.Columns[12].ColumnName = "Ngày hoàn thành";                
                dataGridView1.DataSource = dt;          
            }
            getMPB();
            getMTP();

            cbSearchMPB.Text = MPB;
            tbSearchNPB.Text = NPB;
            cbSearchMTP.Text = TP;
            txtSearchNAMETP.Text = nameTP; 
        }
        
        private void getMPB()
        {
            SQL sql = new SQL();
            string query = "sql_MPB_cb";
            cbSearchMPB.DataSource = sql.getDataTable(query);
            cbSearchMPB.ValueMember = "MPB";
            cbSearchMPB.Text = "";
        }
        private void getMTP()
        {
            SQL sql = new SQL();
            string query = "sql_MTP_cb";
            cbSearchMTP.DataSource = sql.getDataTable(query);
            cbSearchMTP.ValueMember = "MNV";
            cbSearchMTP.Text = "";
        }
        private void updateMTP()
        {
            SQL sql = new SQL();
            string query = "sql_select_MNV_where_MPB";
            cb_pb_mtp.DataSource = sql.getDataTable(query,new SqlParameter("@MPB",tb_pb_npb.Text));
            cb_pb_mtp.ValueMember = "MNV";
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            index = e.RowIndex;
            getValue(index);
            readOnly(true);
            btnAddPB.Enabled = true;
            btnDeletePB.Enabled = true;
            btnEditPB.Enabled = true;
            btnUpdatePB.Enabled = false;            
        }
        private void getValue(int index)
        {
            try
            {
                MPB= tb_pb_mpb.Text = dataGridView1.Rows[index].Cells[1].Value.ToString().Trim();
                NPB= tb_pb_npb.Text = dataGridView1.Rows[index].Cells[0].Value.ToString().Trim();
                PT=tb_pb_pt.Text = dataGridView1.Rows[index].Cells[2].Value.ToString().Trim();
                TP=tb_pb_mtp.Text = dataGridView1.Rows[index].Cells[3].Value.ToString().Trim();
                MCT=tb_pb_mct.Text = dataGridView1.Rows[index].Cells[8].Value.ToString().Trim();
            }
            catch
            {

            }
        }
        private void readOnly(bool check)
        {
            tb_pb_mct.ReadOnly = true;            
            tb_pb_npb.ReadOnly = true;
            tb_pb_pt.ReadOnly = check;
            tb_pb_mpb.ReadOnly = check;
            cb_pb_mtp.Enabled = !check;
        }
        public void clearTextBox()
        {
            tb_pb_mct.Clear();          
            tb_pb_pt.Clear();
            tb_pb_npb.Clear();
            tb_pb_mpb.Focus();
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            AddPB addPB = new AddPB();
            addPB.Show();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (tb_pb_npb.Text == "") MessageBox.Show("Ban van chua chon phong ban nao");
            else
            {
                readOnly(false);
                btnEditPB.Enabled = false;
                btnUpdatePB.Enabled = true;
                updateMTP();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            strQuery = "sql_update_PhongBan";
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter("@MPB", tb_pb_npb.Text);
            pr[1] = new SqlParameter("@NamePB", tb_pb_mpb.Text);
            pr[2] = new SqlParameter("@PT", tb_pb_pt.Text);
            //pr[3] = new SqlParameter("@MTP", cb_pb_mtp.Text);
            if ((tb_pb_npb.Text != "") || (tb_pb_mpb.Text != "") || (tb_pb_pt.Text != ""))
            {
                sql.execute_Query(strQuery, pr);
                updateNhanVien_TP(tb_pb_mtp.Text.Trim(), tb_pb_npb.Text.Trim(), cb_pb_mtp.Text.Trim());
                readOnly(true);
                btnUpdatePB.Enabled = false;
                Search();
            }
            else 
            {
                MessageBox.Show("Please choose Phòng ban !", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            btnUpdatePB.Enabled = false;
            btnEditPB.Enabled = true;            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Search();
            btnAddPB.Enabled = true;
            btnDeletePB.Enabled = true;
            btnEditPB.Enabled = true;
        }
        
        private void btn_pb_del_Click(object sender, EventArgs e)
        {   
            //string table1 = "sql_delete_CongTrinh";
            string table2 = "sql_delete_PhongBan";
           // string updateNV="sql_update_NhanVien_PhongBan";
            string key = "@MPB";
            string value = tb_pb_npb.Text.Trim();
            //sql.execute_Query(updateNV,new SqlParameter(key,value));
            //sql.query_deleteData(table1, key, value);
            if (tb_pb_mpb.Text == "") MessageBox.Show("Ban van chua chon phong ban nao");
            else
            {
                sql.execute_Query(table2, new SqlParameter(key, value));
                Search();
            }
           
            
        }

        private void AddMCT(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                
                contextMenuStrip1.Show(new Point(Cursor.Position.X,Cursor.Position.Y));
            }
        }


        private void thêmCôngTrìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void thêmCôngTrìnhToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (tb_pb_npb.Text == "") MessageBox.Show("Hay chon mot phong ban");
            else
            {
                AddPB_CT add = new AddPB_CT(tb_pb_mpb.Text, tb_pb_npb.Text);
                if (add.ShowDialog() == DialogResult.OK)
                {
                    Search();
                }
            }
        }

      
    }
}
